import React from 'react';
import { Radio } from 'antd';
import Chooser from 'components/chooser';
import { messages } from 'share/common';

class InvoiceCompanySelect extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selRadio: props.selRadio,
      selCompanies: props.selCompanies,
      disabled: props.disabled
    };
  }

  onChange = (e) => {
    const selRadio = e.target.value;
    let { selCompanies } = this.state;
    if (selRadio === 1) {
      selCompanies = [];
    }
    this.setState({ selRadio, selCompanies });
    this.triggerChange({ selRadio, selCompanies });
  };

  handleSelectCompany = (selCompanies) => {
    this.setState({ selCompanies });
    this.triggerChange({ selCompanies });
  };

  triggerChange = (changedValue) => {
    const onChange = this.props.onChange;
    if (onChange) {
      onChange(Object.assign({}, this.state, changedValue));
    }
  };

  componentWillReceiveProps(nextProps) {
    const { selRadio, selCompanies, disabled } = nextProps;
    this.setState({ selRadio, selCompanies, disabled });
  }

  render() {
    const { selRadio, selCompanies, disabled } = this.state;
    return (
      <div>
        <Radio.Group onChange={this.onChange} value={selRadio}>
          <Radio disabled={disabled} value={1}>{messages('setting.key633')/*全部公司*/}</Radio>
          <Radio disabled={disabled} value={2}>{messages('setting.key634')/*部分公司*/}</Radio>
        </Radio.Group>
        {selRadio === 2 &&
        <div style={{ marginTop: '30px' }}>
          <Chooser
            disabled={disabled}
            placeholder={messages('common.please.select')}
            type='available_company'
            labelKey='name'
            valueKey='companyOID'
            onChange={this.handleSelectCompany}
            value={selCompanies}
            single={false}/>
        </div>
        }
      </div>
    );
  }
}

export default InvoiceCompanySelect;
